package com.vfa.fishing.data;

import java.io.IOException;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class AnimalModel {
    private DBAdapter myDAdapter;

    public AnimalModel(Context context) {
        myDAdapter = new DBAdapter(context);
        create();
    }

    private void create() {
        try {
            myDAdapter.createDataBase();
        } catch (IOException ioe) {
            Log.e("LocNNT ",
                    "TbDiet   Unable to create database  =============");
        }
        try {
            myDAdapter.openDataBase();
        } catch (SQLException sqle) {
            Log.e("LocNNT ", "TbDiet   Unable to Open database  =============");
        }

    }

    public void Insert(int ikindAnimal, int isubkindAnimal, int x, int y,
            int direction, int weight, int timetoreset, int isreset,
            int speedX, int speedY) {
        if (!myDAdapter.myDataBase.isOpen()) {
            myDAdapter.openDataBase();
        }
        String sql = "INSERT INTO tbl_animal(ikind_animal,i_subkind,ix,iy,idirection,iweight,iTimeToResetDirect,isReset,ispeedX,ispeedY) VALUES("
                + ikindAnimal
                + ",'"
                + isubkindAnimal
                + "','"
                + x
                + "','"
                + y
                + "','"
                + direction
                + "','"
                + weight
                + "','"
                + timetoreset
                + "','" + isreset + "','" + speedX + "','" + speedY + "')";
        myDAdapter.execSQL(sql);
    }

    public ArrayList<AnimalToRestore> getDataList() {

        if (!myDAdapter.myDataBase.isOpen()) {
            myDAdapter.openDataBase();
        }

        ArrayList<AnimalToRestore> lst = new ArrayList<AnimalToRestore>();
        Cursor result = null;
        String sql = "SELECT * FROM tbl_animal ";

        result = myDAdapter.Query(sql);
        if (result == null) {
            myDAdapter.close();
            return null;
        }
        if (result.moveToFirst()) {
            do {
                try {

                    AnimalToRestore objDB = new AnimalToRestore();
                    objDB.setKindAnimal(result.getInt(0));
                    objDB.setSubKindAnimal(result.getInt(1));
                    objDB.setX(result.getInt(2));
                    objDB.setY(result.getInt(3));
                    objDB.setDirection(result.getInt(4));
                    objDB.setWeight(result.getInt(5));
                    objDB.setTimeToRese(result.getInt(6));
                    objDB.setReset(result.getInt(7));
                    objDB.setVx(result.getInt(8));
                    objDB.setVy(result.getInt(9));
                    lst.add(objDB);
                } catch (SQLiteException ex) {
                    Log
                            .d("Data", "Error when load recipe data"
                                    + ex.toString());
                }
            } while (result.moveToNext());
        }

        if (result != null && !result.isClosed()) {
            result.close();
        }

        myDAdapter.close();
        return lst;
    }

    public void deleteAll() {
        if (!myDAdapter.myDataBase.isOpen()) {
            myDAdapter.openDataBase();
        }
        String DELETE = "delete from tbl_animal";
        myDAdapter.myDataBase.execSQL(DELETE);
    }

}
